local num=tonumber(ARGV[1]);
if (redis.call('exists',KEYS[1])==0) then
    return nil;
end;
local stock = tonumber(redis.call('scard',KEYS[1]));
if (stock == 1) then
    return {'empty'};
end;
local count = num;
if (stock-1 >= num) then
    count = num;
elseif (stock - 1 < num) then
    count = stock-1;
end
local codes = {};
local flag = 0;
for i=1,count do
    local code = redis.call('spop',KEYS[1]);
    if (code == 'empty') then
        code = redis.call('spop', KEYS[1]);
        codes[i] = code;
        flag = 1;
    else
        codes[i] = code;
    end;
end;
if (flag == 1) then
    redis.call('sadd', KEYS[1], 'noDataFlag');
end
return codes;